﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LsKeyboardCore
{
    public partial class FullKeyboardForm : Form
    {
        public FullKeyboardForm(Form refFrom=null)
        {
            this.RefFrom = refFrom;
            InitializeComponent();
        }
        /// <summary>
        /// 关联窗体
        /// </summary>
        public Form RefFrom
        {
            get;
            set;
        }
        private Dictionary<string, string>  keyDic =new Dictionary<string, string>();

        private void FullKeyboardForm_Load(object sender, EventArgs e)
        {
            this.BackColor = KeyBoardAppearance.KeyBoardFormBackColor;
            InitLocation();
            ws.Add(keyQ);
            ws.Add(keyW);
            ws.Add(keyE);
            ws.Add(keyR);
            ws.Add(keyT);
            ws.Add(keyY);
            ws.Add(keyU);
            ws.Add(keyI);
            ws.Add(keyO);
            ws.Add(keyP);
            ws.Add(keyA);
            ws.Add(keyS);
            ws.Add(keyD);
            ws.Add(keyF);
            ws.Add(keyG);
            ws.Add(keyH);
            ws.Add(keyJ);
            ws.Add(keyK);
            ws.Add(keyL);
            ws.Add(keyZ);
            ws.Add(keyX);
            ws.Add(keyC);
            ws.Add(keyV);
            ws.Add(keyB);
            ws.Add(keyN);
            ws.Add(keyM);



            keyDic = new Dictionary<string, string>();
            keyDic.Add("1", "!");
            keyDic.Add("2", "@");
            keyDic.Add("3", "#");
            keyDic.Add("4", "$");
            keyDic.Add("5", "%");
            keyDic.Add("6", "^");
            keyDic.Add("7", "&");
            keyDic.Add("8", "*");
            keyDic.Add("9", "(");
            keyDic.Add("0", ")");
            keyDic.Add("`", "~");
            keyDic.Add("-", "_");
            keyDic.Add("=", "+");
            keyDic.Add("\\", "|");
            keyDic.Add("[", "{");
            keyDic.Add("]", "}");
            keyDic.Add(";", ":");
            keyDic.Add("'", "\"");
            keyDic.Add(",", "<");
            keyDic.Add(".", ">");
            keyDic.Add("/", "?");


            timerCheckTick = new System.EventHandler(timerCheck_Tick);
            this.timerCheck.Tick += timerCheckTick;

            timerCheck_Tick(timerCheck, e);
            timerCheck.Start();
        }

        /// <summary>
        /// 初始化位置
        /// </summary>
        public void InitLocation()
        {
            Rectangle rectangle = Screen.PrimaryScreen.Bounds;
            int x = rectangle.Width / 2 - this.Width / 2;
            int y = rectangle.Height - this.Height - 80;
            this.Location = new Point(x, y);
        }
        /// <summary>
        /// 右侧位置
        /// </summary>
        public void RightLocation()
        {
            Rectangle rectangle = Screen.PrimaryScreen.Bounds;
            int x = rectangle.Width - this.Width;
            int y = rectangle.Height - this.Height - 80;
            this.Location = new Point(x, y);
        }

        private EventHandler timerCheckTick= null;
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void kClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 实现光标在选中目标上
        /// </summary>
        protected override CreateParams CreateParams
        {
            // CreateParams 在 System.Windows.Forms.Controls 命名空间下 
            get
            {
                const int WS_EX_NOACTIVATE = 0x08000000;
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= WS_EX_NOACTIVATE;
                return cp;
            }
        }

        #region 控制窗体拖动
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private bool isMouseDown = false;
        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FullKeyboard_MouseDown(object sender, MouseEventArgs e)
        {
            //if (e.Button == MouseButtons.Left)
            //{
            //    Win32Api.ReleaseCapture();

            //    Win32Api.SendMessage(Handle, 0xA1, 0x02, 0);
            //}
            isMouseDown = true;
        }
        /// <summary>
        /// 鼠标弹起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FullKeyboardForm_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FullKeyboardForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Win32Api.ReleaseCapture();

                    Win32Api.SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
                }
            }
        }
        #endregion

        private List<LabelEx> ws = new List<LabelEx>();

        /// <summary>
        /// 键盘按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void keyCom_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (sender is LabelEx)
                {
                    LabelEx lb = sender as LabelEx;
                    string key = Convert.ToString(lb.Tag);
                    if (!string.IsNullOrEmpty(key))
                    {
                        string key1 = string.Empty;
                        if (keyCom.IsDown)
                        {
                            if (keyShiftLeft.IsDown)
                            {
                                key1 += "+";
                            }
                            if (keyCtrlLeft.IsDown)
                            {
                                key1 += "^";
                            }
                            if (keyAltLeft.IsDown)
                            {
                                key1 += "%";
                            }
                        }

                        switch (key)
                        {
                            case "Caps":
                                this.timerCheck.Tick -= timerCheckTick;
                                keyCaps.IsDown = !keyCaps.IsDown;
                                foreach (LabelEx w in ws)
                                {
                                    if (keyCaps.IsDown)
                                    {
                                        w.Text = w.Text.ToUpper();
                                    }
                                    else
                                    {
                                        w.Text = w.Text.ToLower();
                                    }
                                }
                                SendKeys.SendWait("{CAPSLOCK}");
                                Thread.Sleep(500);
                                this.timerCheck.Tick += timerCheckTick;
                                break;
                            case "Switch":
                                key = "+";
                                SendKeys.Send(key);
                                break;
                            case "Esc":
                                key = key1 + "{ESC}";
                                SendKeys.Send(key);
                                break;
                            case "Tab":
                                key = key1 + "{Tab}";
                                SendKeys.Send(key);
                                break;
                            case "Fn":
                                key = key1 + "{Fn}";
                                SendKeys.Send(key);
                                break;
                            case "Shift":
                                key = "+";
                                SendKeys.Send(key);
                                if (keyCom.IsDown)
                                {
                                    keyShiftRight.IsDown = keyShiftLeft.IsDown = !keyShiftLeft.IsDown;
                                }
                                else
                                {
                                    if (keyShiftRight.IsDown)
                                    {
                                        keyShiftRight.IsDown = keyShiftLeft.IsDown = false;
                                    }
                                }
                                break;
                            case "Ctrl":
                                key = "^";
                                SendKeys.Send(key);
                                if (keyCom.IsDown)
                                {
                                    keyCtrlRight.IsDown = keyCtrlLeft.IsDown = !keyCtrlLeft.IsDown;
                                }
                                else
                                {
                                    if (keyCtrlRight.IsDown)
                                    {
                                        keyCtrlRight.IsDown = keyCtrlLeft.IsDown = false;
                                    }
                                }
                                break;
                            case "Backspace":
                                key = key1 + "{BACKSPACE}";
                                SendKeys.Send(key);
                                break;
                            case "Del":
                                key = key1 + "{DEL}";
                                SendKeys.Send(key);
                                break;
                            case "Alt":
                                key = "%";
                                SendKeys.Send(key);
                                if (keyCom.IsDown)
                                {
                                    keyAltRight.IsDown = keyAltLeft.IsDown = !keyAltLeft.IsDown;
                                }
                                else
                                {
                                    if (keyAltRight.IsDown)
                                    {
                                        keyAltRight.IsDown = keyAltLeft.IsDown = false;
                                    }
                                }
                                break;
                            case "Space":
                                key = key1 + " ";
                                SendKeys.Send(key);
                                break;
                            case "Enter":
                                key = key1 + "{Enter}";
                                SendKeys.Send(key);
                                break;
                            case "↑":
                                key = key1 + "{UP}";
                                SendKeys.Send(key);
                                break;
                            case "←":
                                key = key1 + "{LEFT}";
                                SendKeys.Send(key);
                                break;
                            case "↓":
                                key = key1 + "{DOWN}";
                                SendKeys.Send(key);
                                break;
                            case "→":
                                key = key1 + "{RIGHT}";
                                SendKeys.Send(key);
                                break;
                            case "Com":
                                keyCom.IsDown = !keyCom.IsDown;
                                break;
                            default:
                                if (ws.Contains(lb))
                                {
                                    
                                    key = key1 + key;
                                    SendKeys.Send(key);
                                }
                                else
                                {
                                    string key2 = string.Empty;
                                    if (keyShiftLeft.IsDown)
                                    {
                                        if (keyDic.ContainsKey(key))
                                        {
                                            key2 = "{" + keyDic[key] + "}";
                                        }
                                        else
                                        {
                                            key2 = "{" + key + "}";
                                        }
                                    }
                                    else
                                    {
                                        key2 = "{" + key + "}";
                                    }
                                    key = key1 + key2;
                                    SendKeys.Send(key);
                                }
                                break;
                        }

                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 关闭关联窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FullKeyboardForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if(RefFrom!=null&& !RefFrom.IsDisposed)
            {
                RefFrom.Close();
            }
            timerCheck.Stop();
        }
        private object lock1 = new object();
        /// <summary>
        /// 检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerCheck_Tick(object sender, EventArgs e)
        {
            bool isEnter = false;

            try
            {
                if (Monitor.TryEnter(lock1, 200))
                {
                    isEnter = true;
                    if (Win32Api.CapsLockStatus)
                    {
                        if (!keyCaps.IsDown)
                        {
                            keyCaps.IsDown = true;
                            foreach (LabelEx w in ws)
                            {
                                if (keyCaps.IsDown)
                                {
                                    w.Text = w.Text.ToUpper();
                                }
                                else
                                {
                                    w.Text = w.Text.ToLower();
                                }
                            }
                        }
                    }
                    else
                    {
                        if (keyCaps.IsDown)
                        {
                            keyCaps.IsDown = false;
                            foreach (LabelEx w in ws)
                            {
                                if (keyCaps.IsDown)
                                {
                                    w.Text = w.Text.ToUpper();
                                }
                                else
                                {
                                    w.Text = w.Text.ToLower();
                                }
                            }
                        }
                    }
                }
            }
            catch
            {

            }
            finally
            {
                if (isEnter)
                {
                    Monitor.Exit(lock1);
                }
            }
        }
        /// <summary>
        /// 双击隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelTop_DoubleClick(object sender, EventArgs e)
        {
            if (this.Height >= 555)
            {
                this.Size = new Size(100,64);
                RightLocation();
            }
            else
            {
                this.Size = new Size(1712, 555);
                InitLocation();
            }
        }
        /// <summary>
        /// 大小调整
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelTop_SizeChanged(object sender, EventArgs e)
        {
            if (kClose.IsHandleCreated)
            {
                kClose.Location = new Point(panelTop.Width - kClose.Width - 8, 8);
            }
        }

    }
}
